<?php
/**
 * Created by PhpStorm.
 * User: zhangxinke
 * Date: 2019/6/1
 * Time: 22:02
 */

namespace app\api\model;


use think\Db;

class OrderModel extends BaseModel
{
    public function index(){
        $sort = request()->post('sort');
        return
            $res=Db::name('goods')
              	
                ->where('status',1)
             	->where('sort',$sort)
                ->order('id desc')
                ->select();
    
    }
  	public function search(){
      	$search = request()->post('search');
          	$res = Db::name('goods')
                ->where('status',1)
                ->where('goods','like','%'.$search.'%')
                ->order('id desc')
                ->select();
      	if(empty($res)){
        	$res = Db::name('goods')
                ->where('status',1)
                ->order('id desc')
                ->select();
        }
      return $res;
    }  
    public function ordercheck(){//商品查看
        $id = request()->post('id');
        return
            $res=Db::name('goods')
             	->join('xst_shop','xst_shop.id=xst_goods.shop')
              	->join('xst_shop_type','xst_shop_type.id=xst_shop.type')
              	->join('xst_user','xst_user.id=xst_shop.uid')
                ->where('xst_goods.id',$id)              
              	->field('xst_goods.*,xst_shop.name,xst_shop_type.name tname,xst_user.picture')
                ->find();
    }
  	public function type(){//查询店铺类型
     return
            $res=Db::name('shop_type')
                ->select();
    }
    public function Confirmorder($uid){//确认订单
        $commodity_id = request()->post('commodity_id');
      	$sel=Db::name('goods')
          ->where('id',$commodity_id)
          ->find();
        $number = request()->post('number');
        $price = $number*$sel['price'];
        $aid = request()->post('aid');
        $createtime=time();
        $order_number=$createtime.rand(1000,9999);
        if($commodity_id==''||$price==''||$aid==''||$number=''){//数据不能为null
            return '2';
        }
        $rs=Db::name('balance')//查用户余额
            ->where('uid',$uid)
            ->find();
        $re=Db::name('goods')//查店铺库存
        ->where('id',$commodity_id)
            ->field('stock')
            ->find();
        $circulate=$rs['circulate']-$price;
        if ($circulate<0){//用户余额不足
            return '3';
        }
        $numbers=$re['stock']-$number;//商店库存
        if($numbers<0){//如果店铺库存小于购买数量报错
            return '4';
        }
        $res=Db::name('order')//添加订单
            ->insert([
              	'order_number'=>$order_number,
                'commodity_id' =>$commodity_id,                
                'price' =>$price,
                'aid' =>$aid,
                'number'=>$number,
                'status'=>0,
                'createtime'=>$createtime,
                'order_number'=>$sel['id'],
                'uid'=>$uid
            ]);
        $up=Db::name('goods')
        ->where('id',$commodity_id)
            ->update(['stock'=>$numbers]);//修改店铺的库存
        $ups=Db::name('balance')//查用户余额
        ->where('uid',$uid)
            ->update(['circulate'=>$circulate]);//修改账户余额
            return 1;
    }
  	public function address($uid){//查默认地址
     	return Db::name('area')
            ->where('uid',$uid)
        	->where('flg',1)
            ->find();
    }
  	public function order_list($uid){//订单列表
    	return $res=Db::name('taobao_order')
          	->join('xst_taobao tb','tb.id=xst_taobao_order.commodity_id')
        	->join('xst_taobao_type t','tb.sort=t.id')
            ->where('uid',$uid)
          	->field('xst_taobao_order.*,tb.goods,tb.price univalent,tb.small_img,t.name')
            ->select();
    }
    public function order_check($uid){//订单列表
      $id = request()->post('id');
      return $res=Db::name('taobao_order')
        	->join('xst_taobao tb','bt.id=xst_taobao_order.commodity_id')
        	->join('xst_taobao_type t','tb.sort=t.id')
            ->where('xst_taobao_order.id',$id)
        	->field('xst_taobao_order.*,tb.goods,tb.price univalent,tb.small_img,t.name')
            ->find();
    }
    public function complete($uid){//完成订单
        $id = request()->post('id');
        return Db::name('order')
            ->where('id',$id)
            ->update(['status'=>3]);
    }
}